package com.meihua.baoleyi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.adapters.BankAdapter;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.bean.BankDataBean;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.net.NetHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 银行卡页面
 * Created by liming on 2017/10/18
 */

public class BankActivity extends BaseActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
    @Bind(R.id.bank_list)
    ListView bank_list;
    @Bind(R.id.text_title)
    TextView tvTitle;
    @Bind(R.id.text_back)
    ImageView textBack;
    @Bind(R.id.tv_tip)
    TextView tvTip; //提示
    private List<BankDataBean.DataBean> mBankList = new ArrayList<>();
    private BankAdapter adapter;

    private int type = 0;//   2,提现选择银行卡

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        type = getIntent().getIntExtra("type", 0);
        tvTitle.setText("银行卡");
        adapter = new BankAdapter(this, mBankList, type);
        bank_list.setAdapter(adapter);
        bank_list.setOnItemClickListener(this);
        bank_list.setOnItemLongClickListener(this);
        //返回
        textBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        try {
            showDialog();
        } catch (Exception e) {
        }
        TreeMap<String, String> treeMap = new TreeMap<String, String>();
        treeMap.put("user_id", MyApp.getInstance().getUser_id());
        NetHelper.post(APIServer.BANKSCARD, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                /*请求得到的数据*/
                BankDataBean bean = NetHelper.getBeanForJson(BankDataBean.class, s);
                if (bean == null) return;
                if (bean.getCode().equals("200")) {
                    if (bean.getData().size() == 0) {
                        if (type == 2) {
                            tvTip.setVisibility(View.VISIBLE);
                            tvTip.setText(bean.getMsg());
                        }
                    } else {
                        mBankList.clear();
                        mBankList.addAll(bean.getData());
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    if (type == 2) {
                        tvTip.setVisibility(View.VISIBLE);
                        tvTip.setText(bean.getMsg());
                    }
                    Toast.makeText(BankActivity.this, bean.getMsg(), Toast.LENGTH_SHORT).show();
                }
                dismissDialog();
            }
        });
    }

    /*条目点击事件*/
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (type == 2) {
            Intent in = new Intent();
            in.putExtra("bankCard", (Serializable) mBankList.get(i));
            setResult(1, in);
            finish();
        } else if (type == 0 && i == adapter.getCount() - 1) {
            Intent in = new Intent(this, AddBankVerifyPwdActivity.class);
            startActivity(in);
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
      /*  if (type == 0 && i < mBankList.size()) {
            ClearCacheDialog dlg = new ClearCacheDialog.Builder(this)
                    .setMessage("确定要删除吗？")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            delBank(i);
                            dialogInterface.dismiss();
                        }
                    })
                    .setNativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    })
                    .build();

            dlg.show();
        }*/
        return true;
    }
    /*删除银行卡*/
       /* public void delBank () {
        OkGo.<String>post(URL.getUrl().DELBANK)
                .params("banks_id", mBankList.get(i).getBanks_id())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        BaseBean bean = MyApplication.getInstance().getBeanForJson(BaseBean.class, response.body());
                        if (bean == null) {
                            return;
                        }
                        if (bean.getCode() == 1) {
                            ToastUtils.getInstance().showToast(bean.getMsg());
                            mBankList.remove(i);
                            adapter.notifyDataSetChanged();
                        } else {
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        dialog.dismiss();
                    }
                });
        }*/
}
